/*
 * ParserNMEA.java
 *
 * Created on 13 de junio de 2008, 21:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package project;

/**
 *
 * @author Ana_Pedro
 */
public class ParserNMEA {
    
    /** Creates a new instance of ParserNMEA */
    public ParserNMEA() {
    }

    public static void parseGPGGA(String linea, Position pos) {
        int b1, b2;
        
        b1=linea.indexOf(",");
        b1=linea.indexOf(",",b1+1);
        b1=linea.indexOf(",",b1+1);
        b1=linea.indexOf(",",b1+1);
        b1=linea.indexOf(",",b1+1);
        b1=linea.indexOf(",",b1+1);
        
        if(linea.charAt(b1+1)!='0') {
            b1=linea.indexOf(",",b1+1);
            b1=linea.indexOf(",",b1+1);
            b1=linea.indexOf(",",b1+1);
            b2=linea.indexOf(",",b1+1);
            try {
                pos.altitud=Float.parseFloat(linea.substring(b1+1, b2));
            } catch (NumberFormatException e) {
                pos.altitud=0.0f;
            }
        }
    }
    
    public static void parseGPRMC(String linea, Position pos) {
        int b1, b2;
        String timestampStr;
    
        b1=linea.indexOf(",");
        b2=linea.indexOf(",",b1+1);
        if(b2 > b1) {
            timestampStr=linea.substring(b1+1,b2);
            while((b1=timestampStr.indexOf("."))!=-1)
                timestampStr=timestampStr.substring(0,b1)+timestampStr.substring(b1+1);
        } else
            timestampStr="";
        b1=b2;
        
        if(linea.charAt(b1+1)=='V')
            pos.valida=false;
        else     
            pos.valida=true;
        
        
        b1=linea.indexOf(",",b1+1);
        b2=linea.indexOf(",", b1+1);
        if(b2 > b1)
            try {
                pos.latitudNMEA=Float.parseFloat(linea.substring(b1+1, b2));
            } catch (NumberFormatException e) {
                pos.latitudNMEA=0.0f;
            }
        b1=b2;
        if(linea.charAt(b1+1)=='S')
            pos.latitudNMEA=-pos.latitudNMEA;
        b1=linea.indexOf(",",b1+1);
        b2=linea.indexOf(",", b1+1);
        if(b2 > b1)
            try {
                pos.longitudNMEA=Float.parseFloat(linea.substring(b1+1, b2));
            } catch(NumberFormatException e) {
                pos.longitudNMEA=0.0f;
            }
        b1=b2;
        if(linea.charAt(b1+1)=='W')
            pos.longitudNMEA=-pos.longitudNMEA;
        b1=linea.indexOf(",",b1+1);
        b2=linea.indexOf(",",b1+1);
        if(b2 > b1)
            try {
                pos.velocidad=Float.parseFloat(linea.substring(b1+1, b2));
            } catch (NumberFormatException e) {
                pos.velocidad=0.0f;
            }
        pos.velocidad*=1.852;
        b1=b2;
        b2=linea.indexOf(",", b1+1);
        if(b2 > b1)
            try {
                pos.rumbo=Float.parseFloat(linea.substring(b1+1,b2));
            } catch (NumberFormatException e) {
                pos.rumbo=0.0f;
            }       
        b1=b2;
        b2=linea.indexOf(",", b1+1);
        if(b2 > b1)
            pos.timestamp=linea.substring(b1+1, b2)+timestampStr;
    }    
}
